<%@page import="fr.upmc.bean.Enterprise"%>
<%@page import="fr.upmc.bean.PhoneNumber"%>
<%@page import="org.apache.jasper.tagplugins.jstl.core.ForEach"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%
//is the user allowed to be here?
if(!fr.upmc.utils.SessionTracker.isUserConnected(request))
{
	fr.upmc.utils.SessionTracker.redirectToHome(response);
	return;
}

org.hibernate.Session s = fr.upmc.utils.HibernateUtils.getSessionFactory().openSession();
long id = -1;
try{
	id = Long.parseLong(request.getParameter("id"));
}
catch(Exception e){}

fr.upmc.dao.ContactDAO dao = new fr.upmc.dao.ContactDAO(s);
fr.upmc.bean.Contact contact;

contact = dao.read(id);

/*if(contact == null){
	response.sendRedirect("accueil.jsp");
	return;
}*/
%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <link rel="stylesheet" href="scripts/login/css/style.css" />
    <script src="scripts/jquery/js/jquery-1.6.2.min.js"></script>
    <script src="scripts/jquery/js/jquery-ui-1.8.16.custom.min.js"></script>
    <script src="scripts/customInput.jquery.js"></script>
    <script src="scripts/login/js/login.js"></script>
    <script src="scripts/jquery/jquery.validate.js"></script>
    <link rel="stylesheet" href="scripts/jquery/css/ui-darkness/jquery-ui-1.8.16.custom.css" />
    <link rel="stylesheet" href="scripts/customInput.css" />
	<title>Contact details</title>
	
<script type="text/javascript">
$(document).ready(function(){
    var button = $('#logoutButton');
    var form = $('#logoutForm');
    
    button.click(function(){
    	if(confirm('Are you sure you want to logout?'))
    		form.submit();
    	else
    		return;
    });
    
    $('input:submit').button();
    $('input').customInput();
});
</script>
</head>
<body>
<div id="bar">
        <div id="container">
            <!-- Login Starts Here -->
            <div id="title">MDOC Project - Contact details</div>
            <div id="loginContainer">
            	<a href="accueil.jsp" id="home">Home</a>
                <a href="#" id="logoutButton"><span>Log out</span></a>
                <div style="clear:both"></div>
                <div id="loginBox">                
                    <form id="logoutForm" action="authentification.do" method="post">
                        <fieldset id="body">
                            <input type="hidden" name="action" value="logout" >
                            <input type="submit" id="logout" value="Sign out" />
                        </fieldset>
                    </form>
                </div>
            </div>
            <!-- Login Ends Here -->
		</div>
	</div>
	
<div class="container">
	<br/>
	<%if(contact == null){%>
		Please <b><a href="allContacts.jsp">choose</a></b> a contact first.
	<%}else{%>
	
	<form action="" method="post">
		<fieldset>
			<legend>Basic information</legend>
			<b>First name</b>&nbsp;&nbsp;&nbsp;&nbsp;: <span><%=contact.getFirstName() %></span><br/>
			<b>Last name</b>&nbsp;&nbsp;&nbsp;&nbsp;: <span><%=contact.getLastName() %></span><br/>
			<b>Email</b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;: <span><%=contact.getEmail() %></span>
		</fieldset>
		<br/>
		<fieldset>
			<legend>Address</legend>
			<b>Street</b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;: <span><%=contact.getAddress().getStreet()%></span><br/>
			<b>City</b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;: <span><%=contact.getAddress().getCity() %></span><br/>
			<b>Zip</b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;: <span><%=contact.getAddress().getZip() %></span><br/>
			<b>Country</b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;: <span><%=contact.getAddress().getCountry() %></span>
		</fieldset>
		<br/>
		<fieldset>
			<legend>Phone</legend>
			<%for (fr.upmc.bean.PhoneNumber phone : contact.getProfiles()) {%>
					<b><%=phone.getPhoneKind() %></b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;: <span><%=phone.getPhoneNumber() %></span><br/>
			<%} %>
		</fieldset>
		<br/>
		<fieldset>
			<legend>Contact groups</legend>
			<%for (fr.upmc.bean.ContactGroup group : contact.getBooks()) {%>
				<%=group.getGroupName() %><br/>
			<%} %>
			
		</fieldset>
		<br/>
		<%if(contact instanceof Enterprise){ %>
		<fieldset>
			<legend>Entreprise</legend>
			<b>Siret</b> : <%=((Enterprise)contact).getNumSiret() %>
		</fieldset>
		<%} %>
	</form>
	
	<%} %>
	
	<div id="dialog-modal" title="Updating contact ..">
		<p></p>
	</div>
</div>
</body>
</html>